package edu.hit.pact.pgse.crawler.send;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import edu.hit.pact.pgse.crawler.util.SleepThread;

public class RawPageSendHandler extends IoHandlerAdapter{
	private Log logger;

	public RawPageSendHandler(){
		this.logger = LogFactory.getLog(RawPageSendHandler.class);
		System.out.println("handler is created");
	}
	/*
	 * (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache.mina.core.session.IoSession)
	 */
	public void sessionOpened(IoSession session) {
		logger.error("session open");
	}//end for
	
    public void sessionCreated(IoSession session) {
    	// set idle time to MAX_IDLE_TIME seconds
    	System.out.println("session created");
    }
    
    @Override
	public void sessionClosed(IoSession session) throws Exception {
		logger.error("session is closed normally");
		session.close(true);
	}
    
    /*
     * (non-Javadoc)
     * @see org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache.mina.core.session.IoSession, java.lang.Object)
     */
    @Override
    public void messageReceived(IoSession session, Object message) {
    	logger.error("message receive");
    }
	
    /*
     * (non-Javadoc)
     * @see org.apache.mina.core.service.IoHandlerAdapter#sessionIdle(org.apache.mina.core.session.IoSession, org.apache.mina.core.session.IdleStatus)
     */
	public void sessionIdle(IoSession session, IdleStatus status) {
		logger.info("is idle, do nothing");
	
	}
    
	/*
	 * (non-Javadoc)
	 * @see org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache.mina.core.session.IoSession, java.lang.Throwable)
	 */
    @Override
    public void exceptionCaught(IoSession session, Throwable cause) {
        logger.error("Unexpected exception.", cause);
        // Close connection when unexpected exception is caught.
        session.close(true);
    }
}
